package com.leilei.controller;

import com.leilei.MyObject.myMessage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by zl206 on 2016/10/28.
 */
@ServerEndpoint(value = "/socket")
@Component
public class websocket {
    static Map<Integer,Integer> userCode=new ConcurrentHashMap<>();
    static Map<Integer,Session> sessionMap=new ConcurrentHashMap<>();
    static ObjectMapper objectMapper = new ObjectMapper();
    private Session session;
    private int code=0;
    private int id=0;
    @OnOpen
    public void open(Session session){
        this.session=session;
    }
    @OnMessage
    public void msg(String msg){
        try{
            myMessage myMessage = toMsg(msg);
            Session b = sessionMap.get(myMessage.getId());
            boolean b1 = userCode.containsValue(myMessage.getId());
            if (b!=null&&b1){
                sendMsg(myMessage);
            }else if (b1){
                code=myMessage.getId();
                id=getRealId(code);
                if (id==0){
                    session.close();
                    return;
                }
                sessionMap.put(code,session);
                sendMsg(myMessage);
            }else {
                try {
                    session.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @OnClose
    public void close(){
        userCode.remove(id);
        sessionMap.remove(code);
        try {
            this.session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Integer getRealId(int code){
        for(Map.Entry<Integer,Integer> e:userCode.entrySet()){
            if (e.getValue().equals(code)){
                return e.getKey();
            }
        }
        return 0;
    }


    public void sendMsg(myMessage message){
        try{
            message.setId(id);
            String js=toJson(message);
            Session session=null;
            if(userCode.containsKey(message.getToid())){
                session = sessionMap.get(userCode.get(message.getToid()));
            }
            if (session!=null){
                session.getBasicRemote().sendText(js);
                this.session.getBasicRemote().sendText(js);
            }else {
                message.setContent("对方已经离线");
                this.session.getBasicRemote().sendText(toJson(message));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public myMessage toMsg(String json){
        try {
            return objectMapper.readValue(json, myMessage.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String toJson(myMessage message){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(message);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

}
